package com.dhecp.project.statistics.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.statistics.domain.CommonStatistics;
import com.dhecp.project.statistics.service.CommonStatisticsService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.domain.SysDictionaryForModule;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysDictionaryForModuleService;

/**
 * 查询统计 存储过程通用 控制层
 * @author DH-Xxj
 * @version 2021年3月8日
 */
@Controller
@RequestMapping("/statistics/commonstatistics2")
public class CommonStatistics2Controller extends BaseModuleController{

	private String moduleTable = "";

    @Autowired
    private CommonStatisticsService commonStatisticsService;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    @Autowired
    private SysDictionaryForModuleService dictionaryForModuleService;
    
    /**
     * 查询统计列表
     * @param 
     * @return ModelAndView
     * @author DH-Xxj
     */
    @GetMapping()
    public ModelAndView listPage(@RequestParam Map<String, Object> params){
    	String statistics = "commonstatistics2";
    	String statisticsName = "";	//功能名称
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			ModelAndView mv = this.getModelAndView();
	        mv =  this.gotoModelAndViewErrorPage(params, "moduleTable", "功能唯一标识"); //验证必要参数
	        if (!mv.isEmpty()){ //缺少必要参数时返回错误提示页面
	        	return mv;
	        }
		}
    	params.put("entity", new CommonStatistics()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("prefix","statistics/commonstatistics2"); //页面路径
    	
    	ModelAndView mv = this.openListPage(params);
    	mv.addObject("statistics", statistics);
    	mv.addObject("statisticsName", statisticsName);
        return mv;
    }

    /**
     * 查询统计数据
     * @param params
     * @return TableDataInfo
     * @author DH-Xxj
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			return getDataTable("缺少功能唯一标识【moduleTable】");
		}else{
			moduleTable = params.get("moduleTable").toString();
		}
        List<CommonStatistics> list = new ArrayList<CommonStatistics>();
        Integer countRow = 0;
		Subject userSubject = ShiroUtils.getSubject();
		String ShiroSignList = "";
		SysDictionaryForModule dictionaryForModule = dictionaryForModuleService.queryDictionaryForModuleByModuleTable(moduleTable);
		if (dictionaryForModule != null){
			ShiroSignList = dictionaryForModule.getShiroSignList();
			if("".equals(ShiroSignList) || !userSubject.isPermitted(ShiroSignList)){
				return getDataTable("您没有查询当前功能的权限，请联系管理员分配权限。");
			}
		}
        try {
            list = commonStatisticsService.queryStatisticsList(params);
            if (list != null && list.size() > 0){
            	countRow = list.get(0).getCountRow();
            }
            
        } catch (Exception e) {
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list, countRow);
    }
	
    /**
     * 数据导出Excel
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @Log(title = "查询统计导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
		if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
			moduleTable = "moduleTable";
		}else{
			moduleTable = params.get("moduleTable").toString();
		}
		Subject userSubject = ShiroUtils.getSubject();
		String exportName = "查询统计导出";
		String shiroSignExport = "";
		SysDictionaryForModule dictionaryForModule = dictionaryForModuleService.queryDictionaryForModuleByModuleTable(moduleTable);
		if (dictionaryForModule != null){
			if (dictionaryForModule.getModuleAlias() != null && !"".equals(dictionaryForModule.getModuleAlias())){
				exportName = dictionaryForModule.getModuleAlias() + "导出";
			}else if (dictionaryForModule.getModuleName() != null && !"".equals(dictionaryForModule.getModuleName())){
				exportName = dictionaryForModule.getModuleName() + "导出";
			}
			shiroSignExport = dictionaryForModule.getShiroSignExport();
			if("".equals(shiroSignExport) || !userSubject.isPermitted(shiroSignExport)){
				return AjaxResult.error("您没有导出权限，请联系管理员分配权限。");
			}
		}
		
    	params.put("entity", new CommonStatistics()); 
        List<CommonStatistics> list = commonStatisticsService.queryStatisticsList(params);

        params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<CommonStatistics> util = new ExportExcelUtils<CommonStatistics>(moduleTable,new CommonStatistics());
        return util.exportExcel(list, fieldList, exportName, "export");
    }
}
